<?php
abstract class Query{
    abstract protected function record(array $data);
    public function select(){
        $this->record(['name'=>'后盾人','age'=>23,'mobile'=>17691107518]);
    }
}

class Model extends Query{
    #对象中的属性
    protected $field = [];
    #禁止读取的对象
    protected $deny = [];
    #对外开放的接口
    public function all(){
        $this->select();
        return $this->field;
    }
    protected function record(array $data)
    {
        $this->field = $data;
        // TODO: Implement record() method.
    }

    #获取电话号码
    protected function __mobile(){
        return substr($this->field['mobile'],0,8).'****';
    }

    #获取属性
    public function __get($name)
    {
        if(method_exists($this,'__'.$name)){
            return call_user_func_array([$this,'__'.$name],[]);
        }
        if(isset($this->field[$name])){
            return $this->field[$name];
        }
        throw new Exception("参数错误");
        // TODO: Implement __get() method.
    }

    #设置属性
    public function __set($name, $value)
    {
        // TODO: Implement __set() method.
        if(isset($this->field[$name])){
            $this->field[$name] = $value;
        }else{
            throw new Exception('参数错误');
        }
    }

    #删除属性的值
    public function __unset($name)
    {
        if(!isset($this->field[$name]) || is_array($name,$this->deny)){
            throw new Exception("属性不存在或者禁止操作");
        }
        #如果属性存在，就进行对值进行清空操作
        $this->field[$name] = '';
        // TODO: Implement __unset() method.
    }


    #判断对象是不是存在
    public function __isset($name)
    {
        return isset($this->field[$name]);
        // TODO: Implement __isset() method.
    }

    #调用一个不存在的方法
    public function __call($name, $arguments)
    {
        $action = 'getAttribute'.ucfirst($name);
        if(method_exists($this,$action)){
            return call_user_func_array([$this,$action],$arguments);
        }
        throw new Exception('方法不存在');
        // TODO: Implement __call() method.
    }


    #调用一个不存在的静态方法的时候
    public static function __callStatic($name, $arguments)
    {
        switch ($name){
            case 'mobile':
                $obj = new static();
                $obj->all();
                return call_user_func([$obj,'__call'],$name,$arguments);
                break;
            case 'getAll':
                return call_user_func_array([new static(),'all'],$arguments);
        }
        // TODO: Implement __callStatic() method.
    }

}


class User extends Model{
    #获取指定的属性值
    protected function getAttributeMobile($len=9){
        return substr($this->field['mobile'],0,9).'***';
    }
}

try {
//    $user = new User();
//    $user->all();
//    echo $user->name;
//    echo $user->age;
//    echo $user->mobile;
//   echo  $user->mobile();

//   echo  User::mobile();

    $user = User::getAll();
    print_r($user);
}catch (Exception $e){
    echo $e->getMessage();
}




